package com.zy.pro.shop.web;

import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zy.core.model.PageView;
import com.zy.core.web.BaseController;
import com.zy.pro.shop.model.Cases;
import com.zy.pro.shop.model.Shop;
import com.zy.pro.shop.service.CasesService;
import com.zy.pro.shop.web.form.CasesSearchForm;
import com.zy.pro.system.web.form.MenuForm;
/**
 * 案例管理控制器
 *
 */
@Controller
@RequestMapping("/control/shop/casesController")
public class CasesController extends BaseController {

	private static final Logger logger = Logger.getLogger(CasesController.class);
	
	/**
	 * 案例管理服务类
	 */
	@Resource(name = "casesServiceImpl")
	private CasesService casesService;
	
	/**
	 * 卖家中心 - 列出案例管理
	 */
	@RequestMapping("/list")
	public String list(CasesSearchForm form, ModelMap map) {
		map.addAttribute("reqUrl", "/control/shop/casesController/list.do?caseShopId="+form.getCaseShopId());
		try {
			//设置菜单类型
			MenuForm menuForm = new MenuForm(MenuForm.MENU_TYPE_SELLER,MenuForm.PAR_TYPE_SELLER_CASE,MenuForm.SUB_TYPE_SELLER_CASE_LIST);
			map.addAttribute("menuForm", menuForm);
			
			//设置查询排序
			form.getOrderby().put("caseCreateTime", "desc");
			
			//创建页面对象
			PageView<Cases> pageView = new PageView<Cases>(3, form.getPage());
			form.setFirstResult(pageView.getFirstResult());
			form.setMaxResult(pageView.getMaxresult());
			pageView.setQueryResult(casesService.getData(form));
			map.addAttribute("pageView", pageView);
			map.addAttribute("form", form);
			
			return "/control/jsp/seller/seller_case";
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 卖家中心 - 显示添加案例管理
	 * @param cases
	 * @param map
	 * @return
	 */
	@RequestMapping("/displayAdd")
	public String displayAdd(Cases cases, ModelMap map) {
		try {
			//设置菜单类型
			MenuForm menuForm = new MenuForm(MenuForm.MENU_TYPE_SELLER,MenuForm.PAR_TYPE_SELLER_CASE,MenuForm.SUB_TYPE_SELLER_CASE_ADD);
			map.addAttribute("menuForm", menuForm);
			//判断添加/编辑的操作状态
			map.addAttribute("operateStatus", "add");
			map.addAttribute("cases", cases);
			map.addAttribute("picCount", 0);
			
			return "control/jsp/seller/editCase";
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 *  卖家中心 -添加案例
	 * @param cases
	 * @param picPath
	 * @param map
	 * @param request
	 * @return
	 */
	@RequestMapping("/add")
	public String add(Cases cases,String picPath,ModelMap map, HttpServletRequest request) {
		try {
			
			casesService.addCasesAndPics(cases,picPath, request, logger);
			
			CasesSearchForm form = new CasesSearchForm();
			form.setCaseShopId(cases.getCaseShopId());
			//casesService.save(cases);
			return list(form, map);
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	/*public String add(Cases cases,String picPath0,String picPath1,String picPath2,String picPath3,
			String picPath4,String picPath5,String picPath6,String picPath7,String picPath8,
			String picPath9,ModelMap map, HttpServletRequest request) {
		try {
			
			casesService.addCasesAndPics(cases,picPath0, picPath1, picPath2, picPath3,
					 picPath4, picPath5, picPath6, picPath7, picPath8, picPath9, request, logger);
			
			CasesSearchForm form = new CasesSearchForm();
			form.setCaseShopId(cases.getCaseShopId());
			//casesService.save(cases);
			return list(form, map);
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}*/
	
	/**
	 *  卖家中心 -显示编辑案例管理
	 */
	@RequestMapping("/displayEdit")
	public String displayEdit(CasesSearchForm form, ModelMap map) {
		try {
			//设置菜单类型
			MenuForm menuForm = new MenuForm(MenuForm.MENU_TYPE_SELLER,MenuForm.PAR_TYPE_SELLER_CASE,MenuForm.SUB_TYPE_SELLER_CASE_LIST);
			map.addAttribute("menuForm", menuForm);
			//eg:Cases cases = casesService.find(form.getId());
			//eg:map.addAttribute("entity", cases);
			Cases cases = casesService.find(form.getCaseId());
//			Cases cases = casesService.findCasesAndAllPics(form.getCaseId());
			map.addAttribute("operateStatus", "edit");
			map.addAttribute("cases", cases);
			map.addAttribute("picCount", cases.getPicList().size());
			
			return "control/jsp/seller/editCase";
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 卖家中心 -编辑案例管理
	 */
	@RequestMapping("/edit")
	public String edit(Cases cases,String picPath,ModelMap map, HttpServletRequest request) {
		try {
			casesService.editCasesAndPics(cases,picPath, request, logger);
			CasesSearchForm form = new CasesSearchForm();
			form.setCaseShopId(cases.getCaseShopId());
			return list(form, map);
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
		
	}
	/*public String edit(Cases cases,String picPath0,String picPath1,String picPath2,String picPath3,
			String picPath4,String picPath5,String picPath6,String picPath7,String picPath8,
			String picPath9,ModelMap map, HttpServletRequest request) {
		try {
			casesService.editCasesAndPics(cases,picPath0, picPath1, picPath2, picPath3,
					 picPath4, picPath5, picPath6, picPath7, picPath8, picPath9, request, logger);
			CasesSearchForm form = new CasesSearchForm();
			form.setCaseShopId(cases.getCaseShopId());
			return list(form, map);
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
		
	}*/

	/**
	 * 卖家中心 -编辑案例状态(上架/下架)管理
	 */
	@RequestMapping("/editStatus")
	public void editStatus(Cases cases,ModelMap map, HttpServletRequest request,HttpServletResponse response) {
		try {
			response.setContentType("text/html");
			PrintWriter out;
			out = response.getWriter();
			Cases c = casesService.find(cases.getCaseId());
			if(null!=c){
				Cases ca = new Cases();
				ca.setCaseId(cases.getCaseId());
				ca.setCasesStatus(cases.getCasesStatus());//0:待上架,1:上架,2:下架,3:删除
				casesService.updateBySelective(ca);
				out.print("true");
			}else{
				out.print("false");
			}
		} catch (Exception e) {
			logger.error(e);
		}
	}
}
